﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using System;
using System.ComponentModel;
using System.Drawing;
using DevExpress.Utils;
using DevExpress.XtraEditors;
using XCI.Windows.Helper;

namespace XCI.Windows.Controls
{
    /// <summary>
    /// 窗口顶部控件
    /// </summary>
    //[System.ComponentModel.DesignerCategory("Code")]
    public class CaptionControl : XtraUserControl
    {
        /// <summary>
        /// 
        /// </summary>
        public CaptionControl()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 指定标题颜色
        /// </summary>
        [Description("指定标题颜色")]
        [DefaultValue(null)]
        public Color? CaptionColor
        {
            get => _captionColor;
            set
            {
                _captionColor = value;
                pictureBox1.Invalidate();
            }
        }

        /// <summary>
        /// 指定标题字体
        /// </summary>
        [Description("指定标题字体")]
        [DefaultValue(null)]
        public Font CaptionFont
        {
            get => _captionFont;
            set
            {
                _captionFont = value;
                pictureBox1.Invalidate();
            }
        }

        /// <summary>
        /// 指定标题文字
        /// </summary>
        [Description("指定标题文字")]
        [Browsable(true), DefaultValue("标题文字")]
        public string CaptionText
        {
            get => _captionText;
            set
            {
                _captionText = value;
                pictureBox1.Invalidate();
            }
            //get { return captionImage1.CaptionText; }
            //set { captionImage1.CaptionText = value; }
        }

        private void ResizeControl()
        {
            //this.Height = this.captionImage1.Height + line1.Height;
        }

        private void CaptionControl_Load(object sender, EventArgs e)
        {
            ResizeControl();
        }

        private void CaptionControl_Resize(object sender, EventArgs e)
        {
            ResizeControl();
        }
        private System.Windows.Forms.PictureBox pictureBox1;

        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        private Color? _captionColor;
        private string _captionText;
        private Font _captionFont;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.pictureBox1 = new System.Windows.Forms.PictureBox();
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
            this.SuspendLayout();
            // 
            // pictureBox1
            // 
            this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.pictureBox1.Image = global::XCI.Windows.Properties.Resources.CaptionImage;
            this.pictureBox1.Location = new System.Drawing.Point(0, 0);
            this.pictureBox1.Name = "pictureBox1";
            this.pictureBox1.Size = new System.Drawing.Size(774, 58);
            this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
            this.pictureBox1.TabIndex = 2;
            this.pictureBox1.TabStop = false;
            this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.PictureBox1_Paint);
            // 
            // CaptionControl
            // 
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
            this.Controls.Add(this.pictureBox1);
            this.Name = "CaptionControl";
            this.Size = new System.Drawing.Size(774, 58);
            this.Load += new System.EventHandler(this.CaptionControl_Load);
            this.Resize += new System.EventHandler(this.CaptionControl_Resize);
            ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
            this.ResumeLayout(false);

        }

        #endregion

        private void PictureBox1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
        {
            if (string.IsNullOrEmpty(CaptionText)) return;
            Font font = _captionFont ?? new Font(AppearanceObject.DefaultFont.Name, 16f, FontStyle.Bold);

            var size = WinHelper.GetTextSize(e.Graphics, CaptionText, font);
            int top = (pictureBox1.Height - Convert.ToInt32(size.Height)) / 2;
            int left = 10;//labelCaption.Left;
            e.Graphics.DrawImage(pictureBox1.Image, Width - pictureBox1.Width, 0, pictureBox1.Width, Height);

            //if (LogoImage != null)
            //{
            //    int imgTop = (Height - LogoImage.Height) / 2;
            //    int imgLeft = 10;
            //    left += LogoImage.Width;
            //    e.Graphics.DrawImage(LogoImage, imgLeft, imgTop, LogoImage.Width, LogoImage.Height);
            //}
            var color = Color.White;
            if (CaptionColor.HasValue)
            {
                color = CaptionColor.Value;
            }
            e.Graphics.DrawString(CaptionText, font, new SolidBrush(color), left, top);
        }
    }
}
